'use server';

import { getUserServerSession } from '@/server/auth';
import { createAppSchema } from '@/server/db/validate-schema';
import { serverCaller } from '@/utils/trpc';
import { redirect } from 'next/navigation';

export async function createApp(formData: FormData) {
  const name = formData.get('name');
  const description = formData.get('description');

  const input = createAppSchema
    .pick({
      name: true,
      description: true,
    })
    .safeParse({
      name,
      description,
    });

  if (input.success) {
    const session = await getUserServerSession();

    const newApp = await serverCaller({ session }).apps.createApp(input.data);

    // redirect(`/dashboard/apps/${newApp.id}`);
    return newApp.id;
  } else {
    throw input.error;
  }
}
